<?php 


require "../common/Curl.php";

/**
 *	https://open.esign.cn/doc/detail?id=opendoc%2Fidentity_service%2Feaug1b&namespace=opendoc%2Fidentity_service
 * 	e签宝 认证服务API
 * 	OCR接口+其他----接口顺序排列封装
 *  使用请求签名鉴权方式
 *  下一个------------------ 
 */

/**
 * [imgToBase64 将图片转为base64字符串]
 * @param  [type] $img_file [图片地址]
 * @return [type]           [description]
 */
function imgToBase64($img_file) {
    $img_base64 = '';
    if (file_exists($img_file)) {
        $app_img_file = $img_file; // 图片路径
        $img_info = getimagesize($app_img_file); // 取得图片的大小，类型等
        $fp = fopen($app_img_file, "r"); // 图片是否可读权限
        if ($fp) {
            $filesize = filesize($app_img_file);
            $content = fread($fp, $filesize);
            $file_content = chunk_split(base64_encode($content)); // base64编码
            switch ($img_info[2]) {           //判读图片类型
                case 1: $img_type = "gif";
                    break;
                case 2: $img_type = "jpg";
                    break;
                case 3: $img_type = "png";
                    break;
            }
            //$img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码
            $img_base64 = $file_content;

        }
        fclose($fp);
    }
    return $img_base64; //返回图片的base64
}



//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * [idCardOcr 身份证OCR  对上传的身份证信息面及国徽面图片进行OCR，返回身份证上的信息]
 * @param  [type] $infoImg   [身份证信息面图片BASE64字符串。]
 * @param  [type] $emblemImg [身份证国徽面图片BASE64字符串]
 * @return [type]            [description]
 */
function idCardOcr($infoImg,$emblemImg,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/api/ocr/idcard";
	$head_url = "/v2/identity/auth/api/ocr/idcard";
    $data = [
		"infoImg"=>$infoImg,
		"emblemImg"=>$emblemImg
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}

/**
 * [bankcard 银行卡OCR 对上传的银行卡图片进行OCR，返回银行卡信息]
 * @param  [type] $img    [银行卡正面照照片base64数据。]
 * @return [type]         [description]
 */
function bankcard($img,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/api/ocr/bankcard";
	$head_url = "/v2/identity/auth/api/ocr/bankcard";
    $data = [
		"img"=>$img
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}
$filePath = "./5.jpg";
$img = imgToBase64($filePath);
$a = bankcard($img,$appId,$secret,$url);
var_dump($a);die;


/**
 * [license 营业执照OCR  对上传的营业执照图片进行OCR，返回营业执照上的信息]
 * @param  [type] $img    [营业执照图片BASE64字符串]
 * @return [type]         [description]
 */
function license($img,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/api/ocr/license";
	$head_url = "/v2/identity/auth/api/ocr/license";
    $data = [
		"img"=>$img
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
} 

/**
 * [drivinglicence 驾驶证OCR  上传驾驶证照片，并进行证件信息识别]
 * @param  [type] $image     [驾驶证图片BASE64字符串]
 * @param  [type] $backImage [驾驶证副页图片BASE64字符串]
 * @param  [type] $requestId [请求id，标识一次客户的请求，允许为空]
 * @return [type]            [description]
 */
function drivinglicence($image,$backImage,$requestId,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/api/ocr/drivinglicence";
	$head_url = "/v2/identity/auth/api/ocr/drivinglicence";
    $data = [
		"image"=>$image,
		"backImage"=>$backImage,
		"requestId"=>$requestId
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
} 


/**
 * [drivingPermit 行驶证OCR  上传行驶证照片，并进行证件信息识别]
 * @param  [type] $image     [行驶证图片BASE64字符串]
 * @param  [type] $backImage [行驶证副页图片BASE64字符串]
 * @param  [type] $requestId [请求id，标识一次客户的请求，允许为空]
 * @return [type]            [description]
 */
function drivingPermit($image,$backImage,$requestId,$appId,$secret,$url){
	$urlss = $url."/v2/identity/auth/api/ocr/drivingPermit";
	$head_url = "/v2/identity/auth/api/ocr/drivingPermit";
    $data = [
		"image"=>$image,
		"backImage"=>$backImage,
		"requestId"=>$requestId
	];
    $res = json_encode($data);
    $header = getHeader($data,"POST",$head_url,$appId,$secret,$url);
    $result = sendHttp($method="POST",$urlss,$header,$res);
    return $result;
}



















